a once
#include<iostream>
#include<assert.h>

using std::cin;
using std::cout;
using std::endl;


template<class T=int>
class my_vector
{
public:
    // Vector的迭代器是一个原生指针
        typedef T* iterator;
        typedef const T* const_iterator;
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        const_iterator cbegin()const                    //注意这里的两个const
        {                                               //前面限制返回值不可被修改
            return _start;                              //后面限制传入this const修饰来调用此次函数
        }
        const_iterator cend() const
        {
            return _finish;
        }
        // construct and destroy
        //默认构造
        my_vector()
            :_start(nullptr),
            _finish(nullptr),
            _endOfStorage(nullptr)
        {}
        //n个值构造
        my_vector(int n, const T& value = T())                  // value = T() 默认构造 内置类型也有
            :_start(nullptr),
            _finish(nullptr),
            _endOfStorage(nullptr)
        {
            reserve(n);
            for (int i = 0; i < n; i++)
            {
                push_back(value);
            }
        }
        //迭代器区间构造
        template<class InputIterator>
        my_vector(InputIterator first, InputIterator last)
            :_start(nullptr),
            _finish(nullptr),
            _endOfStorage(nullptr)
        {
            while (first != last)
            {
                push_back(*first);
                first++;
            }
        }
        //拷贝构造
        my_vector(const my_vector<T>& v)
            :_start(nullptr),
            _finish(nullptr),
            _endOfStorage(nullptr)
        {
            const_iterator first = v.cbegin();
            while (first != v.cend())
            {
                push_back(v[first]);
                first++;
            }
        }
        my_vector<T>& operator= (my_vector<T> v)
        {
            swap(v);
            return *this;
        }
        ~my_vector()
        {
            if (_start)
            {
                delete _start;
                _start = _endOfStorage = _finish = nullptr;
            }
        }
        // capacity.
        size_t size() const
        {
            return _finish - _start;
        }
        size_t capacity() const
        {
            return _endOfStorage - _start;
        }
        void reserve(size_t n)
        {
            size_t sz = size();
            if (n > capacity())
            {
                //注意是新new了个对象
                //避免浅拷贝的问题
                T* tmp = new T[n];
                if (_start)
                {
                    for (size_t i = 0; i < size(); i++)
                    {
                        tmp[i] = _start[i];
                    }
                    delete _start;
                }
                _start = tmp;
            }
            _finish = _start + sz;
            _endOfStorage = _start + n;
        }
        void resize(size_t n, const T& value = T())
        {
            if (n > capacity())
            {
                reserve(n);
            }
            if (n > size())
            {
                //1 2 3     8     5
                while (_finish != _start + n)
                {
                    *_finish = value;
                    _finish++;
                }
            }
            else
            {
                _finish = _start + n;
            }
        }
        ///////////////access///////////////////////////////
        T& operator[](size_t pos)
        {
            assert(pos<size());

            return *(_start + pos);
            //return _start[pos];
        }
        const T& operator[](size_t pos)const
        {
            assert(pos < size());

            return *(_start + pos);
        }
        ///////////////modify/////////////////////////////
        void push_back(const T& x)
        {
            if (_finish == _endOfStorage)
            {
                size_t newCapacity = capacity() == 0 ? 4 : capacity() * 2;
                reserve(newCapacity);
            }
            *_finish = x;
            ++_finish;
        }
        void pop_back()
        {
            assert(_finish > _start);
            _finish--;
        }

        void swap(my_vector<T>& v)
        {
            std::swap(_start, v._start);
            std::swap(_finish, v._finish);
            std::swap(_endOfStorage, v._endOfStorage);
        }
        iterator insert(iterator pos, const T& x)
        {
            assert(pos >= _start && pos <= _finish);
            //这样就不用判断扩容，反正这个值也会被覆盖
            push_back(0);

            size_t end = size()+1;
            while (end != (pos-_start))
            {
                _start[end] = _start[end-1];
                end--;
            }
            _start[end] = x;
            return &_start[end];
        }

        iterator erase(iterator pos)
        {
            assert(pos >= begin() && pos < end());

            iterator end = pos + 1;
            while (end!=_finish)
            {
                *(end - 1) = *end;
                end++;
            }
            _finish--;
            return pos;
        }

private:
    //1 2 3 (空间大小为4)
    iterator _start; // 指向数据块的开始
    iterator _finish; // 指向有效数据的尾          end是\0 
    iterator _endOfStorage; // 指向存储容量的尾    
};

//
//int main()
//{
//    my_vector<int> v;
//
//    return 0;
//
